package com.dure.hotmemory.controllers;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.dure.commonlib.util.PreferencesUtils;
import com.dure.commonlib.util.ToastUtils;
import com.dure.hotmemory.bean.Bean;
import com.dure.hotmemory.bean.BindBase;
import com.dure.hotmemory.bean.MyInfo;
import com.dure.hotmemory.bean.PwdUpdate;
import com.dure.hotmemory.bean.UpdateInfo;
import com.dure.hotmemory.bean.Upload;
import com.dure.hotmemory.config.Config;
import com.dure.hotmemory.scenes.UserInfoScene;


import java.io.File;
import java.util.HashMap;

/**
 * Created by dure on 2017/4/21.
 */

public class UserInfoController extends Controller {

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    break;
            }
        }

    };
    UserInfoScene userInfoScene;

    public UserInfoController(UserInfoScene baseScene) {
        super(baseScene);
        userInfoScene = baseScene;
    }

    public void getUserInfo() {
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        else if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));

//        RequestModel requestModel = new RequestModel();
        requestModel.doPost(Config.HOST + Config.USER_INFO, paramMap, MyInfo.class);
    }

    public void bindPhone(String phone, String pwd, String code) {
        HashMap<String, String> paramMap = new HashMap<>();

        paramMap.put("password", pwd);
        paramMap.put("phone", phone);
        paramMap.put("code", code);
        paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));

//        RequestModel requestModel = new RequestModel();
        requestModel.doPost(Config.HOST + Config.BIND_PHONE, paramMap, BindBase.class);
    }


    public void modifyPwd(String currentPwd, String newPwd) {
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));
        paramMap.put("password", currentPwd);
        paramMap.put("new_password", newPwd);

//        RequestModel requestModel = new RequestModel();
        requestModel.doPost(Config.HOST + Config.MODIFY_PWD, paramMap, PwdUpdate.class);
    }

    public void updateUserInfo(String nameStr, String sex, String ageStr, String playerStr, String emailStr) {
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));
        paramMap.put("name", nameStr);
        paramMap.put("sex", sex);

        if (!TextUtils.isEmpty(ageStr)) {
            paramMap.put("age", ageStr);
        }

        if (!TextUtils.isEmpty(playerStr)) {
            paramMap.put("player", playerStr);
        }

        if (!TextUtils.isEmpty(emailStr)) {
            paramMap.put("email", emailStr);
        }

//        RequestModel requestModel = new RequestModel();
        requestModel.doPost(Config.HOST + Config.UPDATE_INFO, paramMap, UpdateInfo.class);
    }

    public void uploadImg(File file){
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));

        requestModel.doPost(Config.HOST + Config.UPLOAD, paramMap, file, Upload.class);
    }

    @Override
    public void handleRequestSuccess(Bean bean) {
        if (bean instanceof MyInfo) {
            MyInfo userInfo = (MyInfo) bean;
            if (userInfo.status == 1) {
                userInfoScene.setUser(userInfo.data.get(0));
            }
        } else if (bean instanceof UpdateInfo) {
            UpdateInfo updateInfo = (UpdateInfo) bean;
            if (updateInfo.status == 1) {
                ToastUtils.show(activity, updateInfo.msg);
                userInfoScene.updateInfoBackHandle();
            } else {
                ToastUtils.show(activity, updateInfo.msg);
            }
        } else if (bean instanceof Upload) {
            Upload upload = (Upload) bean;
            if (upload.status == 1) {
                userInfoScene.handleUploadBack(upload.image);
            }
        } else if (bean instanceof PwdUpdate) {
            PwdUpdate findPwd = (PwdUpdate) bean;
            ToastUtils.show(activity, findPwd.msg);
        } else if (bean instanceof BindBase) {
            if (((BindBase) bean).status == 1) {
                PreferencesUtils.putString(activity, "phone", ((BindBase) bean).phone);
                userInfoScene.handleBindSuccessBack();
            }
            ToastUtils.show(activity, ((BindBase) bean).msg);
        } else {
            super.handleRequestSuccess(bean);
        }
    }
}
